var defaultfare;
var faretemps;
$(function () {
    try {
        var showproduct = {
            "boxid": "showbox",
            "sumid": "showsum",
            "boxw": 350,//大图width
            "boxh": 350,//大图height
            "sumw": 58,//小图width
            "sumh": 58,//小图height
            "sumi": 5,//小图margint-left
            "sums": 5,//小图列表数
            "sumsel": "sel",
            "sumborder": 1,//列表边框，没有边框填写0，边框在css中修改
            "lastid": "showlast",
            "nextid": "shownext"
        };//参数定义
        $.ljsGlasses.pcGlasses(showproduct);//方法调用，务必在加载完后执行
        $("#J_tab_info .tab-con-nav li").tab({tabBd: "#J_tab_info .tab-con-bd-item", _event: "click"});
    } catch (w) {
    }
    $w.httpRequest({
        url: apiroot + '/order/loadFaretemp',
        data: {},
        ok: function (ret) {
            defaultfare = ret.Data.defaultfare
            faretemps = ret.Data.faretemps;

            if (currentAreaInfo && currentAreaInfo.currentProvinceName)
                computefare(currentAreaInfo.currentProvinceName, currentAreaInfo.currentCityName, parseInt($("[name='amount']").val()));
        }
    })

    setBrowsepriceid($("[name='priceid']").val(), $("[name='clscode']").val());
    $("#maylikemore").attr("href", $("#maylikemore").attr("href") + "?clscode=" + getBrowsclscode());

    loadMaylike(6, loadMaylikeok);

});

function chooseCityok() {
    computefare(currentAreaInfo.currentProvinceName, currentAreaInfo.currentCityName, parseInt($("[name='amount']").val()));
}
//计算运费
function computefare(p, c, amount) {
    var faretemp;
    for (var i = 0; i < faretemps.length; i++) {
        if (faretemps[i].province.indexOf(p) != -1 && faretemps[i].city.indexOf(c) != -1) {
            faretemp = faretemps[i];
            break;
        }
    }
    if (!faretemp) faretemp = defaultfare;
    var fare = 0;
    if (faretemp) {
        if (amount <= faretemp.startnum) fare = faretemp.startmoney;
        else {
            var pix = (faretemp.nextmoney / faretemp.nextnum).tofixed(2);
            var diff = amount - faretemp.startnum;//续件
            fare = (diff * pix).tofixed(2);
        }
    }
    $("#fare").text("￥" + fare);
}

function addamount() {
    var txt = $("[name='amount']");
    var n = parseInt(txt.val());
    n++;
    txt.val(n);
    inputAmount(txt);
}
function lessenamount() {
    var txt = $("[name='amount']");
    var n = parseInt(txt.val());
    n--;
    txt.val(n);
    inputAmount(txt);
}
function handleOften(obj) {
    if ($("i", $(obj)).get(0).className.indexOf("oftenflase") != -1) {
        $w.httpRequest({
            url: apiroot + "/order/addOften",
            data: {"priceid": $("[name='priceid']").val()},
            ok: function (ret) {
                $(obj).html('<i class="oftentrue"></i>已加入常购清单<b></b>');
                // $(obj).parent().get(0).className = 'qingdan-btn2';
                // $(obj).removeAttr("onclick");
                // $(obj).attr("onclick", "delOften(this);");
            }
        })
    } else {
        $w.httpRequest({
            url: apiroot + "/order/delOften",
            data: {"priceid": $("[name='priceid']").val()},
            ok: function (ret) {
                $(obj).html('<i class="oftenflase"></i>加入常购清单<b></b>');
                // $(obj).parent().get(0).className='qingdan-btn';
                //  $(obj).removeAttr("onclick");
                //  $(obj).attr("onclick","addOften(this);");
            }
        })
    }
}
function addtocart(event, obj) {

    var url = ctx + "/order/addToCart?pid=" + $("[name='priceid']").val() + "&amount=" + $("[name='amount']").val();
    $w.addCartItem(event, obj, url);

}
// function delOften(obj){
//   $w.httpRequest({
// 		url:apiroot+"/order/delOften",
// 		data:{"priceid":$("[name='priceid']").val()},
// 		ok:function(ret){
// 			$(obj).html("加入常购清单<b></b>");
// 			$(obj).parent().get(0).className='qingdan-btn';
//               $(obj).removeAttr("onclick");
//               $(obj).attr("onclick","addOften(this);");
// 		}
// 	})
// }

function loadMaylikeok(datas) {
    var panel = $("#maylike");
    for (var i = 0; i < datas.list.length; i++) {
        var data = datas.list[i];
        var h = '<li class="Related-item">'
            + '<div class="Related-pic"><a href="' + ctx + '/product/info?priceid=' + data.id + '" target="_blank"><img src="' + getRandomFileserver() + data.photo + '" width="130" height="130"  onerror="this.src=\'' + ctx + '/website/images/noimg.jpg\'"/></a></div>'
            + '<div class="Related-text">';
        if (isuserauth && islogin) h += '<p><strong>￥' + data.price + '</strong></p>';
        else if (!islogin) h += '<p><font class="red">登录可见</font></p>';
        else h += '<p><font class="red">审核通过可见</font></p>';
        h += '<p><a href="' + ctx + '/product/info?priceid=' + data.id + '" target="_blank">' + data.productname + ' ' + data.name + ' ' + (data.b_guige || '') + '</a></p>'
            + '</div>'
            + '</li>';
        panel.append(h);
    }
}

function productfavorite(obj) {
    if ($w.checkLogin(true, ctx + "/product/info?priceid=" + $("[name='priceid']").val())) {
        if ($("i", $(obj)).get(0).className.indexOf("fa-star-o") != -1) {
            $w.httpRequest({
                url: apiroot + '/product/favorite',
                data: {priceid: $("[name='priceid']").val(), "action": 0},
                ok: function (ret) {
                    $(obj).html('<i class="fa fa-star"></i>已收藏')
                }
            })
        } else {
            $w.httpRequest({
                url: apiroot + '/product/favorite',
                data: {priceid: $("[name='priceid']").val(), "action": 1},
                ok: function (ret) {
                    $(obj).html('<i class="fa fa-star-o"></i>收藏');
                }
            })
        }
    }
}

function addtongz(obj) {
    $(obj).text("已添加到货提醒")
}